import api from '../../../api';

export default {
  namespace: 'domeM',
  state: {
    dataSource: [
      {
        id: '1',
        name: 'Brown',
        address: 'New York',
      },
      {
        id: '2',
        name: 'Green',
        address: 'London',
      },
      {
        id: '3',
        name: 'Black',
        address: 'Sidney',
      },
    ],
  },
  reducers: {
    addSource(state, payload) {
      let _state = JSON.parse(JSON.stringify(state));
      _state.dataSource = [payload.data, ..._state.dataSource];
      return _state;
    },
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  effects: {
    *deleteBtn({ payload }, { put, call }) {
      yield put({
        type: 'setState',
        payload: {
          dataSource: payload.dataSource,
        },
      });
    },
    // 获取系列产品列表
    *getSeriesProducts({ payload, callback }, { call, put, select }) {
      const res = yield call(api.getSeriesProducts, {});
      let resultData = [];

      if (res && res.code == 0) {
        resultData = res.data.dataSource;
      } else {
        message.error((res && res.msg) || '获取系列产品失败!');
      }
      callback(resultData);
    },
  },
};
